using System.Linq;
using JetBrains.ReSharper.Feature.Services.CSharp.Generate;
using JetBrains.ReSharper.Feature.Services.Generate;
using JetBrains.ReSharper.Psi.CSharp;
using JetBrains.ReSharper.Psi.Util;
using MugenMvvmToolkit.ReSharper.Infrastructure;
using NuGet;

namespace MugenMvvmToolkit.ReSharper.ViewModels.Generators
{
    [GeneratorElementProvider(GeneratorKindConstants.GenerateModelProperties, typeof(CSharpLanguage))]
    public class ModelPropertiesGeneratorProvider : GeneratorProviderBase<CSharpGeneratorContext>
    {
        #region Overrides of GeneratorProviderBase<CSharpGeneratorContext>

        public override double Priority
        {
            get { return 0; }
        }

        public override void Populate(CSharpGeneratorContext context)
        {
            var classLikeDeclaration = context.ClassDeclaration;
            var modelType = ToolkitExtensions.GetEntityModelType(classLikeDeclaration);
            if (modelType == null)
                return;
            var classType = modelType.GetClassType();
            if (classType == null)
                return;
            var missingProperties = ModelPropertyGenerator.GetMissingProperties(classLikeDeclaration, classType);
            context.ProvidedElements.AddRange(missingProperties.Select(property => new GeneratorDeclaredElement(property)));
        }

        #endregion
    }
}